package com.ruoyi.accountant.service.impl;

import com.ruoyi.accountant.domain.AccountDetail;
import com.ruoyi.accountant.mapper.AccountDetailMapper;
import com.ruoyi.accountant.service.AccountDetailService;
import com.ruoyi.web.web.form.AccountDetailForm;
import com.ruoyi.accountant.domain.AccountDetailExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/***
 * 账户交易记录明细
 */
@Transactional
@Service
public class AccountDetailServiceImpl implements AccountDetailService {

    @Autowired
    private AccountDetailMapper accountDetailMapper;

    @Override
    public void save(AccountDetail accountDetail) {
        accountDetailMapper.insert(accountDetail);
    }

    @Override
    public List<AccountDetail> findAll() {
        AccountDetailExample accountDetailExample = new AccountDetailExample();
        return accountDetailMapper.selectByExample(accountDetailExample);
    }

    @Override
    public List<AccountDetail> findByForm(AccountDetailForm form) {

//        com.github.pagehelper.Page<AccountDetail> page = new com.github.pagehelper.Page<>();

        AccountDetailExample example = new AccountDetailExample();

        AccountDetailExample.Criteria criteria = example.createCriteria()
                .andTransactionDateGreaterThanOrEqualTo(form.getTransactionStartDate())
                .andTransactionDateLessThanOrEqualTo(form.getTransactionEndDate());

        if(form.getOfAccount() != null && form.getOfAccount() != -1) {
            criteria.andOfAccountEqualTo(form.getOfAccount());
        }

        example.setOrderByClause("createtime desc");

        return accountDetailMapper.selectByExample(example);
    }

    @Override
    public AccountDetail findById(Long aid) {
        return accountDetailMapper.selectByPrimaryKey(aid);
    }
}
